package com.hxzon.aixzsecretary.ui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import com.hxzon.aixzsecretary.net.IPuserData;
import com.hxzon.aixzsecretary.net.UserDialog;
import com.hxzon.aixzsecretary.util.AixzObservable;
import com.hxzon.aixzsecretary.util.AixzSensor;

public class UserList extends JPanel implements Observer{
    private AixzSensor sensor;
    private JList userList;
    private DefaultListModel model;

    public UserList(){
        JPanel cPane=this;
        cPane.setLayout(null);
        userList=new JList();
        userList.setToolTipText("双击与对方发送信息");
        userList.setBounds(0,2,240,180);
        cPane.add(userList);

        model=new DefaultListModel();
        userList.setModel(model);
        userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        userList.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount()==2){
                    String s=(String)userList.getSelectedValue();
                    if(s==null) return;
                    StringBuffer bs=new StringBuffer(s);
                    int split=s.indexOf(":");
                    String userName=bs.substring(0,split);
                    String ip=bs.substring(split+2,bs.length());
                    System.out.println(userName);
                    System.out.println(ip);
                    new UserDialog(userName,ip);
                }
            }
        });
    }

    public void observe(AixzObservable object){
        this.sensor=object.getSensor();
        sensor.addObserver(this);
    }

    public void update(Observable subject, Object object){
        //IPuserData s=(IPuserData)sensor.getData();
        IPuserData s=(IPuserData)((AixzSensor)subject).getData();
        String newhost=s.ip;
        if(s.state.equals("add")){
            if(notContains(s.user+":"+s.ip))
                addItem(s.user+":"+s.ip);
               }
         if(s.state.equals("del")){
               removeItem(s.user+":"+s.ip);
        }
    }

    public void addItem(Object userdata){
        DefaultListModel userData=(DefaultListModel)(userList.getModel());
        userData.addElement(userdata);
    }

    public void removeItem(Object userdata){
        DefaultListModel userData=(DefaultListModel)(userList.getModel());
        userData.removeElement(userdata);
    }

    public boolean notContains(Object userdata){
        DefaultListModel userData=(DefaultListModel)(userList.getModel());
        return !userData.contains(userdata);
    }

    public static void main(String[] args){
        JFrame frame=new JFrame();
        JPanel contentPane= (JPanel) frame.getContentPane();
        contentPane.setLayout(null);

        UserList userList=new UserList();
        userList.setBounds(2,2,240,180);
        contentPane.add(userList);
        userList.addItem("123");

        frame.setSize(245, 400);
        frame.setResizable(true);
        frame.setTitle("sectetary");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//
    }


}
